<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>User Login JSP</title>
<script type="text/javascript">
function register()
{
	document.forms[0].action = "http://localhost:8080/HomeFinders/reg_form.jsp";    // First target
    document.forms[0].target = "roommate_req";    // Open in a iframe
    document.forms[0].submit();   
    return true;
}
</script>
<%
String error=request.getParameter("error");
if(error==null || error=="null"){
 error="";
}%>
<%
System.out.println("in login");
session = request.getSession();
if (session.isNew()) {
    System.out.println ("new session");
} else {
	System.out.println ("old session");
	System.out.println("username: "+session.getAttribute("username"));
	System.out.println("pw: "+session.getAttribute("pw"));
	if(session.getAttribute("username") != null && session.getAttribute("username")!="")
		response.sendRedirect("success.jsp");
} 

%>

<script type="text/javascript">
    function trim(s) 
    {
        return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
    }

    function validate()
    {
        if(trim(document.frmLogin.sUserName.value)=="")
        {
          alert("Please fill out username.");
          document.frmLogin.sUserName.focus();
          return false;
        }
        else if(trim(document.frmLogin.sPwd.value)=="")
        {
          alert("Please fill out password.");
          document.frmLogin.sPwd.focus();
          return false;
        }
    }
</script>
</head>

<body>
<div><%=error%></div>
<div id="Results_frmLogin">
<form name="frmLogin" onSubmit="return validate();" action="doLogin.jsp" method="post">
User Name <input type="text" name="sUserName" /><br />
Password <input type="password" name="sPwd" /><br />
<input type="submit" name="sSubmit" value="Submit" />
<input type="button" name="sRegister" value="Register" onclick = "register()"/><br>
<a href="http://localhost:8080/HomeFinders/forgetpw.jsp">Forgot username or password</a>
</form></div>
</body>
</html>